package com.leyou.item.service.impl;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpecService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {
    private final SpecGroupService specGroupService;
    private final SpecParamService specParamService;

    public SpecServiceImpl(SpecGroupService specGroupService,SpecParamService specParamService){
        this.specGroupService = specGroupService;
        this.specParamService = specParamService;
    }


    @Override
    public List<SpecGroupDTO> listGroupByCategory(Long cid) {

        //select *from  tb_spec_group where category_id=#{cid}
    return SpecGroupDTO.convertEntityList(this.specGroupService.query().eq("category_id", cid).list());
    }

    @Override
    public List<SpecParamDTO> listSpecParams(Long gid, Long cid, Boolean searching) {
        //select * from tb_spec_param where [group_id = #{gid}] and [category_id = #{cid} ]and [ searching =  #{searching}]

        List<SpecParam> specParams = this.specParamService.query()
                .eq(null != gid, "group_id", gid)
                .eq(null != cid, "category_id", cid)
                .eq(null != searching, "searching", searching)
                .list();


        return SpecParamDTO.convertEntityList(specParams);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        this.specGroupService.save(specGroup);
    }

    @Override
    public void updteSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        this.specGroupService.updateById(specGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSpecParam(SpecParamDTO specParamDTO) {

        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);

        this.specParamService.save(specParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        this.specParamService.updateById(specParam);
    }

    @Override
    public List<SpecGroupDTO> listSpecGroupWithSpecParam(Long cid) {

        //根据分类id查询对应的规格参数组集合，
        List<SpecGroupDTO> specGroupDTOS = this.listGroupByCategory(cid);

        //需要根据规格参数组id查询对应的组内参数
//        specGroupDTOS.forEach(specGroupDTO -> {
//
//            //select * from tb_spec_param where group_id = #{gid}
//            specGroupDTO.setParams(this.listSpecParams(specGroupDTO.getId(),null,null));
//        });

        //根据分类id查询当前分类下所有的规格参数
        List<SpecParamDTO> specParamDTOS = this.listSpecParams(null, cid, null);


        //根据组id，对所有的规格参数进行分组，得到一个map，map的key就是groupId，map的value是具有共同组id的规格参数的集合
        Map<Long,List<SpecParamDTO>> paramMap =
                specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(paramMap.get(specGroupDTO.getId()));
        });

        return specGroupDTOS;
    }

}
